﻿using Microsoft.AspNet.SignalR.Client;
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace PingYou.Test
{
    public class SendSignalR
    {

        #region HubConnection 连接推送信息
        private static HubConnection _conn = null;
        private static IHubProxy _proxy = null;
        private static string HubConnectionUrl = "http://10.10.1.199:8009/";
        public string HubConnection()
        {
            try
            {
                _conn = new HubConnection(HubConnectionUrl);
                _proxy = _conn.CreateHubProxy("signalRHub");

            }
            catch (Exception ex)
            {

            }
            return HubConnectionUrl;
        }
        #endregion

        // GET api/<controller>
        public void GetInfoData()
        {
            this.HubConnection();
            this.SendNotice("10008819", "2", "消息发送");
        }

        private dynamic SendNotice(string userID, string buid, string message, int messageType = 1)
        {
            try
            {
                _conn.Start().Wait();
            }
            catch
            {
                try
                {
                    _conn = new HubConnection(HubConnectionUrl);
                    _proxy = _conn.CreateHubProxy("signalRHub");
                    _conn.Start().Wait();
                }
                catch (Exception ex)
                {

                }
            }
            try
            {
                Dictionary<string, string> dict = new Dictionary<string, string> {
            { "TaskManageCourseID",buid },
            { "Message",message },
            { "MessageType",messageType.ToString() }
        };
                string json = JsonConvert.SerializeObject(dict, Formatting.Indented);

                _proxy.Invoke("sendNotity", new string[] { userID }, json);
            }
            catch (Exception ex)
            {
                return new { error = 1, message = ex.Message };
            }
            return new { error = 0 };
        }
    }
}
